package com.ztw.yygh.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ztw.yygh.common.result.Result;
import com.ztw.yygh.common.utils.AuthContextHolder;
import com.ztw.yygh.enums.OrderStatusEnum;
import com.ztw.yygh.model.order.OrderInfo;
import com.ztw.yygh.order.service.OrderService;
import com.ztw.yygh.vo.order.OrderCountQueryVo;
import com.ztw.yygh.vo.order.OrderQueryVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/order/orderInfo")
public class orderApiController {
    @Resource
    private OrderService orderService;

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }

    //取消预约
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable Long orderId){
        Boolean isOrder=orderService.cancelOrder(orderId);
        return Result.ok(isOrder);
    }
    /*
    生成订单 => orderInfo(yygh-hosp)->get(SignKey)->orderInfo(yygh-manage) and
    schedule(yygh-manage)->orderInfo(yygh-hosp)->send message and schedule(yygh-hosp)
    */
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result saveOrders(@PathVariable String scheduleId,
                             @PathVariable Long patientId) {

        return Result.ok(orderService.saveOrder(scheduleId, patientId));
    }

    //根据id查询订单消息
    @GetMapping("auth/getOrders/{orderId}")
    public Result getOrders(@PathVariable String orderId) {
        OrderInfo orderInfo = orderService.getOrder(orderId);
        return Result.ok(orderInfo);
    }

    @ApiOperation(value = "获取分页列表")
    @GetMapping("/auth/{page}/{limit}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit,
            @ApiParam(name = "orderCountQueryVo", value = "查询对象", required = false)
            OrderQueryVo orderQueryVo,
            HttpServletRequest request) {
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        IPage<OrderInfo> pageModel = orderService.selectPage(pageParam, orderQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取订单状态")
    @GetMapping("getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }
}
